package org.spring.springboot.base;

import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.joda.time.DateTimeFieldType;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.springframework.cglib.beans.BeanMap;

import java.math.BigDecimal;
import java.util.*;

/**
 * @author zhaobo
 * @version 1.0
 * @date 2019/6/14 17:00
 */
public class BaseCommon {


        public static String DATE_FULL = "yyyy-MM-dd HH:mm:ss";
        public static String DATE_MIN = "yyyy-MM-dd HH:mm";
        public static String DATE_HOURS = "yyyy-MM-dd HH";
        public static String DATE_DAY = "yyyy-MM-dd";
        public static String DATE_MONTH = "yyyy-MM";
        public static String DATE_YEAR = "yyyy";
        public static String DATE_YEARMONTH = "yyyyMM";
        public static String DATE_CLINGMINU = "yyyyMMddHHmm";
        public static String DATE_CLINGDAY = "yyyyMMdd";
        public static String DATE_CLINGSECONDS = "yyyyMMddHHmmss";
        public static String Def_Chars[] = {"${row}"};

        /*  提示消息 */
        public static String PARAM_MSG = "操作失败,请检查参数";



    /**
     * 将对象装换为map
     *
     * @param bean
     * @return
     */
    public static <T> Map<String, Object> beanToMap(T bean) {
        Map<String, Object> map = Maps.newHashMap();
        if (bean != null) {
            BeanMap beanMap = BeanMap.create(bean);
            for (Object key : beanMap.keySet()) {
                map.put(key + "", beanMap.get(key));
            }
        }
        return map;
    }

    /**
     * 将map装换为javabean对象
     *
     * @param map
     * @param bean
     * @return
     */
    public static <T> T mapToBean(Map<String, Object> map, T bean) {
        BeanMap beanMap = BeanMap.create(bean);
        beanMap.putAll(map);
        return bean;
    }


        /**
         * @param dateTimeStr
         * @param formatStr
         * @return
         */
        public static DateTime parse(String dateTimeStr, String formatStr) {
            DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(formatStr);
            return dateTimeFormatter.parseDateTime(dateTimeStr);
        }

        /**
         * @param dateTimeStr
         * @return
         */
        public static DateTime parse(String dateTimeStr) {
            return parse(dateTimeStr, DATE_FULL);
        }

        /**
         * 格式化时间 - yyyy-MM-dd HH:mm:ss
         *
         * @param date
         * @return
         */
        public static String format(Date date) {
            return format(date, DATE_FULL);
        }

        /**
         * 格式化时间 - yyyy-MM-dd HH:mm:ss
         *
         * @param date
         * @return
         */
        public static String format(DateTime date) {
            return format(date.toDate(), DATE_FULL);
        }

        /**
         * 格式化时间
         *
         * @param date
         * @return
         */
        public static String format(Date date, String formatStr) {
            return new DateTime(date.getTime()).toString(formatStr);
        }

        /**
         * 将开始时间戳格式的时间转化为字符串格式
         *
         * @param formatStr
         * @return
         */
        public static String getNowTime(String formatStr) {
            return new DateTime(System.currentTimeMillis()).toString(formatStr);
        }


        /**
         * 将开始时间戳格式的时间转化为字符串格式
         *
         * @param startTime
         * @return
         */
        public String getTime(String startTime) {
            return new DateTime(Long.valueOf(startTime)).toString(DATE_FULL);
        }

        /**
         * 将开始时间戳格式的时间转化为字符串格式
         *
         * @param startTime
         * @param formatStr
         * @return
         */
        public String getTime(String startTime, String formatStr) {
            return new DateTime(Long.valueOf(startTime)).toString(formatStr);
        }

        /**
         * @param val
         * @return
         */
        public String getStrValDefZero(Object val) {
            return getStrVal(val, "0");
        }

        /**
         * JdbcTemplate listMap
         *
         * @param val
         * @return
         */
        public String getStrVal(Object val) {
            return getStrVal(val, "");
        }


        /**
         * JdbcTemplate listMap--date长度超过19只取19位
         *
         * @param val
         * @return
         */
        public String getStrVal19(Object val) {
            String v = getStrVal(val, "");
            if (v.length() >= 19) {
                return v.substring(0, 19);
            }
            return v;
        }

        /**
         * JdbcTemplate listMap
         *
         * @param val
         * @return
         */
        public int getIntVal(Object val) {
            return Integer.valueOf(getStrVal(val, "0"));
        }

        /**
         * JdbcTemplate listMap
         *
         * @param val
         * @param def
         * @return
         */
        public String getStrVal(Object val, String def) {
            String v = String.valueOf(val);
            if (StringUtils.isNotEmpty(v)) {
                return "null".equalsIgnoreCase(v) ? def : v;
            }
            return v;
        }

        /**
         * @param val
         * @return
         */
        public double getDoubleVal(Object val) {
            return Double.valueOf(getStrVal(val, "0"));
        }

        /**
         * JdbcTemplate listMap 当参数等于0d 的时候，返回null
         *
         * @param val
         * @return
         */
        public Double getDoubleZeroValDefNull(Object val) {
            if (0d == getDoubleVal(val)) {
                return null;
            } else {
                return getDoubleVal(val);
            }

        }


        /**
         * 根据开始天 结束天.返回中间小时
         *
         * @param startTime
         * @param endTime
         * @return
         */
        public List<Map<String, String>> getHours(String startTime, String endTime) {
            List<Map<String, String>> result = new ArrayList<>();
            DateTime start = parse(startTime).withHourOfDay(0);
            DateTime end = parse(endTime).plusHours(1).withHourOfDay(0).plusSeconds(-1);
            while (start.isBefore(end)) {
                Map<String, String> map = new HashMap<>();
                int h = start.get(DateTimeFieldType.hourOfHalfday());
                map.put(start.toString(DATE_HOURS), (h < 10 ? ("0" + h) : ("" + h)));
                start = start.plusHours(1);
                result.add(map);
            }
            return result;
        }

        /**
         * 根据开始天 结束天.返回中间小时
         *
         * @param startTime
         * @param endTime
         * @return
         */
        public List<Map<String, String>> getHours(Date startTime, Date endTime) {
            return getHours(format(startTime), format(endTime));
        }





        /**
         * 向上取小数点多少位
         *
         * @param val
         * @param scale
         * @return
         */
        public double roundUp(double val, int scale) {
            BigDecimal decimal = new BigDecimal(val);
            BigDecimal setScale = decimal.setScale(scale, BigDecimal.ROUND_HALF_UP);
            return setScale.doubleValue();
        }

        /**
         * 向下取小数点多少位
         *
         * @param val
         * @param scale
         * @return
         */
        public double roundDown(double val, int scale) {
            BigDecimal decimal = new BigDecimal(val);
            BigDecimal setScale = decimal.setScale(scale, BigDecimal.ROUND_HALF_DOWN);
            return setScale.doubleValue();
        }

        /**
         * 根据,分隔,返回('id1','id2')
         *
         * @param inputStr
         * @return
         */
        public String getCommaSplitToStr(String inputStr) {
            return getCommaSplit(inputStr, true);
        }

        /**
         * 根据,分隔,返回(id1,id2)
         *
         * @param inputStr
         * @return
         */
        public String getCommaSplitToInt(String inputStr) {
            return getCommaSplit(inputStr, false);
        }

        /**
         * 根据,分隔
         *
         * @param inputStr
         * @param isStr
         * @return
         */
        private String getCommaSplit(String inputStr, boolean isStr) {
            String result = "";
            if (StringUtils.isNotBlank(inputStr)) {

                String[] inputStrSplit = inputStr.split(",");
                for (String str : inputStrSplit) {
                    if (StringUtils.isNotBlank(str)) {
                        if (isStr) {
                            result += "'" + str + "',";
                        } else {
                            result += "" + str + ",";
                        }
                    }
                }
                if (StringUtils.isNotBlank(result)) {
                    result = " ( " + result.substring(0, result.length() - 1) + " ) ";
                }
            }
            return result;
        }

    }
